
import {arrayToHeavy} from '../utils/arrayOperator'

const mutations = {
    

    Login(state,payload){//将歌曲从播放列表中移除
        
       state.loginState = true;
       state.userInfo = payload;

    },
    Logout(state){//退出登录
        
        state.loginState = false;
     },

    AddSong(state,payload){//往播放列表中添加一首歌曲


        
        const newSongIndex = state.playList.findIndex((item)=> item.id === payload.id);

        
        //如果播放列表中存在当前歌曲,则将其移除并重新添加到列表最顶部
        newSongIndex === -1 ? state.playList.unshift(payload) : (state.playList.splice(newSongIndex,1)&& state.playList.unshift(payload))
        
        state.isPlay = true;

        //播放当前新增歌曲
        state.currentIndex = 0;    
        
        
    },
    RemoveSong(state,payload){//将歌曲从播放列表中移除
        
        console.log("移除歌曲",payload);
        
        const songIndex = state.playList.findIndex((item)=> item.id === payload);

        console.log('移除歌曲的index',songIndex);
        
        state.playList.splice(songIndex,1);
    },
    ChangePlayState(state,payload){//改变播放状态
        
        console.log("payload",payload);
        
        if(payload === undefined){
            state.isPlay = !state.isPlay;
        }else{
            state.isPlay = payload
        }
       
    },
    SwitchSong(state,payload){//切换歌曲
        if(payload === -1){//前一首
            state.currentIndex = (state.currentIndex -1) < 0 ? (state.playList.length-1) : (state.currentIndex -1)
            console.log("当前播放索引", state.currentIndex);
            
        }else if(payload === 1){//后一首
            state.currentIndex =   (state.currentIndex + 1) % state.playList.length;
            console.log("当前播放索引", state.currentIndex);
        }else{//指定歌曲id

            const songIndex = state.playList.findIndex((item)=> item.id === payload);

            state.currentIndex =  songIndex;

        }
        
    },
    ClearPlayList(state){//清空播放列表
       
        state.playList = [];
        
    },
    PlayAll(state,payload){

        state.playList = payload;
    },
    addAllSongs(state,payload){
        console.log("添加歌曲",payload);
        
        const list = arrayToHeavy(state.playList.concat(payload),'id') ;
        
        console.log("去重后的list",list);
        
        state.playList = list ;
    },
}

  export default mutations